# Copyright (c) 2017, 2018,
# Dmitry Salychev <darkness.bsd@gmail.com>,
# Alexander Salychev <ppsalex@rambler.ru> et al.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#     * Redistributions of source code must retain the above copyright
#       notice, this list of conditions and the following disclaimer.
#     * Redistributions in binary form must reproduce the above copyright
#       notice, this list of conditions and the following disclaimer in the
#       documentation and/or other materials provided with the distribution.
#     * Neither the name of the <organization> nor the
#       names of its contributors may be used to endorse or promote products
#       derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR
# ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
#
# CMake script to check MISRA C:2012 rules by "make misra" command.
#
file(GLOB_RECURSE C_FILES "@CMAKE_SOURCE_DIR@/src/*.c")
#file(GLOB_RECURSE C_FILES "@CMAKE_CURRENT_SOURCE_DIR@/../tests/*.c")

# Load modules from our source tree too
set(CMAKE_MODULE_PATH "@CMAKE_CURRENT_SOURCE_DIR@/../cmake")

# Look for libraries, packages, etc.
find_package(cppcheck REQUIRED)
find_package(PythonInterp REQUIRED)

if (CPPCHECK_FOUND AND PYTHONINTERP_FOUND)
	message(STATUS "cppcheck executable: ${CPPCHECK_EXECUTABLE}")
	message(STATUS "cppcheck version: ${CPPCHECK_VERSION}")
	message(STATUS "python executable: ${PYTHON_EXECUTABLE}")
	message(STATUS "python version: ${PYTHON_VERSION_STRING}")

	foreach(C_FILE ${C_FILES})
		message(STATUS "[FILE]: ${C_FILE}")

		# Create dump for a file
		get_filename_component(WORKING_DIR ${C_FILE} DIRECTORY)
		execute_process(
			COMMAND ${CPPCHECK_EXECUTABLE} --dump ${C_FILE}
			RESULT_VARIABLE res
			WORKING_DIRECTORY ${WORKING_DIR}
		)

		# Run cppcheck addon to check MISRA C rules
		if (EXISTS "@CMAKE_CURRENT_SOURCE_DIR@/MISRA_C_2012.txt")
			execute_process(
				COMMAND ${PYTHON_EXECUTABLE} @CMAKE_CURRENT_SOURCE_DIR@/misra.py --rule-texts=@CMAKE_CURRENT_SOURCE_DIR@/MISRA_C_2012.txt ${C_FILE}.dump
				RESULT_VARIABLE res
				WORKING_DIRECTORY ${WORKING_DIR}
			)
		else()
			execute_process(
				COMMAND ${PYTHON_EXECUTABLE} @CMAKE_CURRENT_SOURCE_DIR@/misra.py ${C_FILE}.dump
				RESULT_VARIABLE res
				WORKING_DIRECTORY ${WORKING_DIR}
			)
		endif()

		# Remove dump
		if (EXISTS ${C_FILE}.dump)
			file(REMOVE ${C_FILE}.dump)
		endif()
	endforeach()
endif()
